<template>
  <div class="header-container">
    <div class="header-left">
      <router-link to="/" class="logo-link">
        <font-awesome-icon icon="graduation-cap" class="logo-icon" />
        <span class="logo-text">学习学分银行</span>
      </router-link>
      
      <ul class="header-nav">
        <li><router-link to="/" class="nav-link"><font-awesome-icon icon="home" /> <span class="nav-text">首页</span></router-link></li>
        <li><router-link to="/earn-points" @click="toLogin" class="nav-link"><font-awesome-icon icon="graduation-cap" /> <span class="nav-text">获取学分</span></router-link></li>
        <li class="nav-dropdown">
          <a href="#" class="nav-link"><font-awesome-icon icon="book" /> <span class="nav-text">课程</span> <font-awesome-icon icon="chevron-down" class="dropdown-arrow" /></a>
          <div class="dropdown-content">
            <router-link to="/search-course" v-if="user && user.userType === 0" class="dropdown-item"><font-awesome-icon icon="user-plus" /> 加入课程</router-link>
            <router-link to="/new-course" v-if="user && user.userType === 1" class="dropdown-item"><font-awesome-icon icon="plus-circle" /> 创建课程</router-link>
            <router-link to="/my-course" class="dropdown-item"><font-awesome-icon icon="list-ul" /> 我的课程</router-link>
          </div>
        </li>
        <li class="nav-dropdown">
          <a href="#" class="nav-link"><font-awesome-icon icon="search" /> <span class="nav-text">资源管理</span> <font-awesome-icon icon="chevron-down" class="dropdown-arrow" /></a>
          <div class="dropdown-content">
            <router-link to="/search-resource" @click="toLogin" class="dropdown-item"><font-awesome-icon icon="search" /> 查找资源</router-link>
            <router-link to="/share-resource" @click="toLogin" class="dropdown-item"><font-awesome-icon icon="share-alt" /> 共享资源</router-link>
            <router-link to="/my-resource" @click="toLogin" class="dropdown-item"><font-awesome-icon icon="folder-open" /> 我的资源</router-link>
          </div>
        </li>
        <li><a href="#" @click.prevent="goToCreditCertification" class="nav-link"><font-awesome-icon icon="certificate" /> <span class="nav-text">学分认证</span></a></li>
        <li><router-link to="/forum" @click="toLogin" class="nav-link"><font-awesome-icon icon="comments" /> <span class="nav-text">论坛</span></router-link></li>
      </ul>
    </div>
    
    <div class="header-search">
      <input 
        v-model="searchKeyword" 
        @keyup.enter="goSearchCourse" 
        placeholder="搜索课程..." 
        class="search-input"
      />
      <button @click="goSearchCourse" class="search-button">
        <font-awesome-icon icon="search" />
      </button>
    </div>
    
    <div class="header-right">
      <div v-if="user == null" class="header-login">
        <button @click="setShowModal(true)" class="login-button">
          <font-awesome-icon icon="user" /> 登录/注册
        </button>
      </div>
      
      <div v-if="user != null" class="user-info">
        <AvatarImage
          :size="40"
          @click="goToUserCenter"
          class="user-avatar"
        />
        <span class="username">{{user.username}}</span>
        <div class="dropdown">
          <font-awesome-icon icon="chevron-down" class="dropdown-icon" />
          <div class="dropdown-menu">
            <router-link to="/user-center" class="dropdown-menu-item">个人页面</router-link>
            <router-link to="/" @click.prevent="logout" class="dropdown-menu-item">退出登录</router-link>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 登录/注册模态框 -->
    <div v-if="showModal" class="modal" @click.self="closeModal">
      <div class="modal-content" @click.stop>
        <div class="role-tab">
          <button :class="{ active1: activeTab === 'student' }" @click="switchTab('student')">
            <font-awesome-icon icon="user-graduate" /> 我是学生
          </button>
          <button :class="{ active2: activeTab === 'teacher' }" @click="switchTab('teacher')">
            <font-awesome-icon icon="chalkboard-teacher" /> 我是教师
          </button>
        </div>
        <!-- 其他模态框内容保持不变 -->
         <div v-if="errorMessage" class="error-message">
         <font-awesome-icon icon="exclamation-circle" /> {{ errorMessage }}
        </div>
        <div class="sub-tab">
          <button :class="{ active: activeSubTab === 'account' }" @click="activeSubTab = 'account'; clearErrors()">
            <font-awesome-icon icon="user-plus" /> 注册
          </button>
          <button :class="{ active: activeSubTab === 'password' }" @click="activeSubTab = 'password'; clearErrors()">
            <font-awesome-icon icon="lock" /> 密码登录
          </button>
        </div>

        <div v-if="activeTab === 'student'" class="tab-content">
          <div v-if="activeSubTab === 'account'" class="sub-tab-content">
            <input type="email" v-model="studentEmail" placeholder="请输入邮箱" required>
            <input type="password" v-model="studentPassword" placeholder="请输入密码" required>
            <PasswordStrengthMeter :password="studentPassword" />
            <input type="password" v-model="studentConfirmPassword" placeholder="请再次输入密码" required>
            <div class="captcha-container">
              <input type="text" v-model="emailCaptcha" placeholder="请输入邮箱验证码" required>
              <button @click="sendEmailCaptcha" :disabled="isSending">
               <font-awesome-icon icon="envelope" />
                {{ isSending ? `${countdown}秒后重试` : '发送验证码' }}
              </button>
            </div>
            <button @click="register('student')"><font-awesome-icon icon="user-plus" /> 注册</button>
          </div>

          <div v-if="activeSubTab === 'password'" class="sub-tab-content">
            <input type="text" v-model="studentAccount" placeholder="请输入邮箱/昵称" required>
            <input type="password" v-model="studentPassword" placeholder="请输入密码" required>
            <div class="captcha-container">
              <input type="text" v-model="imageCaptcha" placeholder="请输入图片验证码" required>
              <img :src="captchaImage" @click="refreshCaptcha" alt="验证码" class="captcha-image">
            </div>
            <button @click="login('student')"><font-awesome-icon icon="sign-in-alt" /> 登录</button>

            <a href="#" @click.prevent="showForgotPasswordView" class="forgot-password-link">忘记密码？</a>
          </div>

          <div v-if="activeSubTab === 'forgot-password'" class="sub-tab-content">
            <h4>找回密码</h4>
            <input type="email" v-model="forgotPasswordEmail" placeholder="请输入您的注册邮箱" required>
            <button @click="handleForgotPassword"><font-awesome-icon icon="paper-plane" /> 发送重置链接</button>
            <a href="#" @click.prevent="activeSubTab = 'password'" class="forgot-password-link">返回登录</a>
          </div>
        </div>

        <div v-if="activeTab === 'teacher'" class="tab-content">
          <div v-if="activeSubTab === 'account'" class="sub-tab-content">
            <input type="email" v-model="teacherEmail" placeholder="请输入邮箱" required>
            <input type="password" v-model="teacherPassword" placeholder="请输入密码" required>
            <PasswordStrengthMeter :password="teacherPassword" />
            <input type="password" v-model="teacherConfirmPassword" placeholder="请再次输入密码" required>
            <div class="captcha-container">
              <input type="text" v-model="emailCaptcha" placeholder="请输入邮箱验证码" required>
              <button @click="sendEmailCaptcha" :disabled="isSending">
                <font-awesome-icon icon="envelope" />
                {{ isSending ? `${countdown}秒后重试` : '发送验证码' }}
              </button>
            </div>
            <button @click="register('teacher')"><font-awesome-icon icon="user-plus" /> 注册</button>
          </div>

          <div v-if="activeSubTab === 'password'" class="sub-tab-content">
            <input type="text" v-model="teacherAccount" placeholder="请输入邮箱/昵称" required>
            <input type="password" v-model="teacherPassword" placeholder="请输入密码" required>
            <div class="captcha-container">
              <input type="text" v-model="imageCaptcha" placeholder="请输入图片验证码" required>
              <img :src="captchaImage" @click="refreshCaptcha" alt="验证码" class="captcha-image">
            </div>
            <button @click="login('teacher')"><font-awesome-icon icon="lock" /> 登录</button>

            <a href="#" @click.prevent="showForgotPasswordView" class="forgot-password-link">忘记密码？</a>
          </div>

          <div v-if="activeSubTab === 'forgot-password'" class="sub-tab-content">
            <h4>找回密码</h4>
            <input type="email" v-model="forgotPasswordEmail" placeholder="请输入您的注册邮箱" required>
            <button @click="handleForgotPassword"><i class="fas fa-paper-plane"></i> 发送重置链接</button>
            <a href="#" @click.prevent="activeSubTab = 'password'" class="forgot-password-link">返回登录</a>
          </div>
        </div>

        <div v-if="activeTab === 'student'">
          <div class="third-party-divider">
            <span>或通过以下方式登录</span>
          </div>
          <div class="third-party-login">
            <button @click="handleAlipayLogin" class="alipay-btn">
              <img src="https://cdn.simpleicons.org/alipay/1677FF" alt="alipay logo" class="alipay-icon"/>
              支付宝
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>


<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { userServie } from '@/services/user_service'
import { tokenManager } from '@/utils/token'
import { userManager } from '@/utils/user'
import { getShowModal, setShowModal } from '@/utils/eventBus'
import { ElMessage } from 'element-plus'
import PasswordStrengthMeter from "@/views/PasswordStrengthMeter.vue"
import AvatarImage from './AvatarImage.vue'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
const router = useRouter()
const searchKeyword = ref('')

// 搜索课程
const goSearchCourse = () => {
  const keyword = searchKeyword.value.trim()
  if (keyword) {
    router.push({ path: '/search-course', query: { q: keyword } })
  } else {
    router.push({ path: '/search-course' })
  }
}

// 模态框相关状态
const showModal = getShowModal()
const activeTab = ref('student')
const activeSubTab = ref('account')

// 表单数据
const studentAccount = ref('')
const studentEmail = ref('')
const studentPassword = ref('')
const studentConfirmPassword = ref('')
const teacherAccount = ref('')
const teacherEmail = ref('')
const teacherPassword = ref('')
const teacherConfirmPassword = ref('')

// 验证码相关
const emailCaptcha = ref('')
const imageCaptcha = ref('')
const captchaImage = ref('')
const generatedCaptcha = ref('')
const generatedEmailCode = ref('')
const isSending = ref(false)
const countdown = ref(60)

// 用户信息
const user = userManager.getUserInfo()
const errorMessage = ref('')

// 忘记密码
const forgotPasswordEmail = ref('')

// 关闭模态框
const closeModal = () => {
  showModal.value = false
}

// 生成随机验证码
const generateRandomCode = (length = 4) => {
  const chars = '0123456789'
  let result = ''
  for (let i = 0; i < length; i++) {
    result += chars.charAt(Math.floor(Math.random() * chars.length))
  }
  return result
}

// 刷新验证码
const refreshCaptcha = () => {
  generatedCaptcha.value = generateRandomCode()
  const canvas = document.createElement('canvas')
  canvas.width = 100
  canvas.height = 40
  const ctx = canvas.getContext('2d')

  ctx.fillStyle = '#f5f5f5'
  ctx.fillRect(0, 0, canvas.width, canvas.height)

  for (let i = 0; i < 5; i++) {
    ctx.strokeStyle = `rgb(${Math.floor(Math.random() * 150)}, ${Math.floor(Math.random() * 150)}, ${Math.floor(Math.random() * 150)})`
    ctx.beginPath()
    ctx.moveTo(Math.random() * canvas.width, Math.random() * canvas.height)
    ctx.lineTo(Math.random() * canvas.width, Math.random() * canvas.height)
    ctx.stroke()
  }

  ctx.font = '24px Arial'
  ctx.fillStyle = '#333'
  ctx.textAlign = 'center'
  ctx.textBaseline = 'middle'

  for (let i = 0; i < generatedCaptcha.value.length; i++) {
    ctx.save()
    ctx.translate(30 + i * 20, 20)
    ctx.rotate((Math.random() - 0.5) * 0.4)
    ctx.fillText(generatedCaptcha.value[i], 0, 0)
    ctx.restore()
  }

  captchaImage.value = canvas.toDataURL('image/png')
}

// 发送邮箱验证码
const sendEmailCaptcha = () => {
  const email = activeTab.value === 'student' ? studentEmail.value : teacherEmail.value

  if (!email) {
    errorMessage.value = '请输入邮箱地址'
    return
  }

  if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
    errorMessage.value = '请输入有效的邮箱地址'
    return
  }

  userServie.emailVerificationCode(email, (res) => {
    if (res.code === 1000) {
      generatedEmailCode.value = res.msg
      isSending.value = true
      countdown.value = 60
      const timer = setInterval(() => {
        countdown.value--
        if (countdown.value <= 0) {
          clearInterval(timer)
          isSending.value = false
        }
      }, 1000)
    } else {
      errorMessage.value = res.msg || '验证码发送失败，请稍后重试'
    }
  })
}

// 注册
const register = (role) => {
  const email = role === 'student' ? studentEmail.value : teacherEmail.value
  const password = role === 'student' ? studentPassword.value : teacherPassword.value
  const confirmPassword = role === 'student' ? studentConfirmPassword.value : teacherConfirmPassword.value
  
  if (!email || !password || !confirmPassword || !emailCaptcha.value) {
    errorMessage.value = '请填写所有必填项'
    return
  }

  if (password !== confirmPassword) {
    errorMessage.value = '两次输入的密码不一致'
    return
  }

  if (emailCaptcha.value !== generatedEmailCode.value) {
    errorMessage.value = '邮箱验证码错误'
    return
  }
  
  const date = new Date()
  const userType = role === 'student' ? 0 : 1
  const name = `${role}_${date.getTime()}`
  const user = {
    username: name,
    email: email,
    password: password,
    createTime: new Date(),
    updateTime: new Date(),
    userType: userType
  }
  
  userServie.register(user, (res) => {
    if (res.code === 1000) {
      activeSubTab.value = 'password'
      ElMessage.success('注册成功，请登录')
    } else {
      errorMessage.value = res.msg || '注册失败，请稍后重试'
    }
  })
}

// 登录
const login = (role) => {
  const account = role === 'student' ? studentAccount.value : teacherAccount.value
  const password = role === 'student' ? studentPassword.value : teacherPassword.value

  if (!account || !password || !imageCaptcha.value) {
    errorMessage.value = '请填写所有必填项'
    return
  }

  if (imageCaptcha.value.toLowerCase() !== generatedCaptcha.value.toLowerCase()) {
    errorMessage.value = '图片验证码错误'
    refreshCaptcha()
    return
  }

  const user = {
    userType: role === 'student' ? 0 : 1,
    account: account,
    password: password,
  }

  userServie.login(user, (res) => {
    if (res.code === 1000) {
      tokenManager.setToken(res.data.token)
      userManager.setUserInfo(res.data.user, res.data.student, res.data.teacher)
      location.reload()
      showModal.value = false
      clearErrors()
    } else {
      errorMessage.value = res.msg || '登录失败'
      refreshCaptcha()
    }
  })
}

// 切换标签页
const switchTab = (tab) => {
  activeTab.value = tab
  activeSubTab.value = 'account'
  clearErrors()
  refreshCaptcha()
}

// 清除错误信息
const clearErrors = () => {
  errorMessage.value = ''
}

// 退出登录
const logout = async () => {
  await userManager.logout()
  router.push("/")
  //location.reload()
  setTimeout(() => {
    location.reload()
  }, 300)
}

// 检查登录状态
const toLogin = () => {
  if(userManager.getUserInfo() == null){
    router.push("/")
    userManager.toLogin()
    ElMessage.error("请先登录") 
  }
}

// 跳转用户中心
const goToUserCenter = () => {
  router.push('/user-center')
}

// 跳转学分认证
const goToCreditCertification = () => {
  if (tokenManager.getToken()) {
    router.push('/user-center/credit-certification')
  } else {
    userManager.toLogin()
    ElMessage.error("请先登录")
    router.push('/')
  }
}

// 忘记密码相关
const showForgotPasswordView = () => {
  activeSubTab.value = 'forgot-password'
  clearErrors()
}

const handleForgotPassword = () => {
  if (!forgotPasswordEmail.value) {
    errorMessage.value = '请输入邮箱地址'
    return
  }
  
  userServie.forgotPassword(forgotPasswordEmail.value, (res) => {
    if (res.code === 1000) {
      ElMessage.success(res.msg)
      activeSubTab.value = 'password'
    } else {
      errorMessage.value = res.msg || '发送失败，请稍后重试'
    }
  })
}

// 支付宝登录
const handleAlipayLogin = () => {
  const APP_ID = '2021000148677978'
  const REDIRECT_URI = 'http://localhost:8080/api/alipay/callback'
  const encodedRedirectUri = encodeURIComponent(REDIRECT_URI)
  const authUrl = `https://openauth-sandbox.dl.alipaydev.com/oauth2/publicAppAuthorize.htm?app_id=${APP_ID}&scope=auth_user&redirect_uri=${encodedRedirectUri}`
  window.location.href = authUrl
}

// 初始化验证码
onMounted(() => {
  refreshCaptcha()
})
</script>

<style scoped>
/* 基础样式重置 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
}

.header-container {
  width: 100%;
  height: 70px;
  background-color: #ffffff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 2rem;
  box-shadow: 0 2px 15px rgba(0, 0, 0, 0.08);
  position: fixed;
  top: 0;
  z-index: 1000;
  border-bottom: 1px solid #f0f0f0;
}

/* 左侧区域样式 */
.header-left {
  display: flex;
  align-items: center;
  gap: 2rem;
}

.logo-link {
  display: flex;
  align-items: center;
  text-decoration: none;
  color: #2c3e50;
  font-weight: 700;
  font-size: 1.4rem;
  transition: all 0.3s ease;
}

.logo-link:hover {
  transform: translateY(-2px);
}

.logo-icon {
  color: #409EFF;
  font-size: 1.8rem;
  margin-right: 0.5rem;
}

.logo-text {
  background: linear-gradient(135deg, #409EFF, #67C23A);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* 导航菜单样式 */
.header-nav {
  list-style: none;
  display: flex;
  gap: 1.2rem;
}

.nav-link {
  color: #606266;
  text-decoration: none;
  font-weight: 500;
  font-size: 0.95rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  transition: all 0.3s ease;
  padding: 0.8rem 0.5rem;
  position: relative;
}

.nav-link:hover {
  color: #409EFF;
}

.nav-link i {
  font-size: 1rem;
}

.nav-text {
  margin-left: 0.3rem;
}

/* 下拉菜单样式 */
.nav-dropdown {
  position: relative;
}

.dropdown-arrow {
  font-size: 0.7rem;
  margin-left: 0.3rem;
  transition: transform 0.3s ease;
}

.nav-dropdown:hover .dropdown-arrow {
  transform: rotate(180deg);
}

.dropdown-content {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  background-color: white;
  min-width: 180px;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
  z-index: 1001;
  padding: 0.5rem 0;
  animation: fadeInDropdown 0.2s ease;
  border: 1px solid #ebeef5;
}

@keyframes fadeInDropdown {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

.nav-dropdown:hover .dropdown-content {
  display: block;
}

.dropdown-item {
  color: #606266;
  padding: 0.7rem 1.2rem;
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 0.7rem;
  font-size: 0.9rem;
  transition: all 0.2s ease;
}

.dropdown-item:hover {
  background-color: #f5f7fa;
  color: #409EFF;
  padding-left: 1.4rem;
}

.dropdown-item i {
  width: 18px;
  text-align: center;
  font-size: 0.9rem;
}

/* 搜索框样式 */
.header-search {
  display: flex;
  align-items: center;
  background-color: #f5f7fa;
  border-radius: 25px;
  padding: 0.3rem 1rem;
  border: 1px solid #e4e7ed;
  transition: all 0.3s ease;
  width: 280px;
}

.header-search:hover {
  border-color: #c0c4cc;
}

.header-search:focus-within {
  border-color: #409EFF;
  box-shadow: 0 0 8px rgba(64, 158, 255, 0.2);
}

.search-input {
  border: none;
  outline: none;
  margin-right: 0.5rem;
  width: 100%;
  font-size: 0.9rem;
  background: transparent;
  color: #606266;
  padding: 0.5rem 0;
}

.search-input::placeholder {
  color: #c0c4cc;
}

.search-button {
  background: none;
  border: none;
  cursor: pointer;
  color: #909399;
  font-size: 1rem;
  transition: all 0.3s ease;
  padding: 0.3rem;
}

.search-button:hover {
  color: #409EFF;
  transform: scale(1.1);
}

/* 右侧区域样式 */
.header-right {
  display: flex;
  align-items: center;
  gap: 1.5rem;
}

/* 登录按钮样式 */
.login-button {
  background: linear-gradient(135deg, #409EFF, #66b1ff);
  color: white;
  border: none;
  border-radius: 25px;
  padding: 0.6rem 1.5rem;
  cursor: pointer;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  transition: all 0.3s ease;
  box-shadow: 0 2px 6px rgba(64, 158, 255, 0.3);
}

.login-button:hover {
  background: linear-gradient(135deg, #66b1ff, #409EFF);
  transform: translateY(-2px);
  box-shadow: 0 4px 10px rgba(64, 158, 255, 0.4);
}

.login-button:active {
  transform: translateY(0);
}

/* 用户信息样式 */
.user-info {
  display: flex;
  align-items: center;
  position: relative;
  color: #303133;
  gap: 0.7rem;
  cursor: pointer;
  padding: 0.3rem 0.7rem;
  border-radius: 20px;
  transition: all 0.3s ease;
}

.user-info:hover {
  background-color: #f5f7fa;
}

.user-avatar {
  transition: transform 0.3s ease;
}

.user-info:hover .user-avatar {
  transform: scale(1.05);
}

.username {
  font-weight: 500;
  font-size: 0.95rem;
  max-width: 100px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.dropdown {
  position: relative;
}

.dropdown-icon {
  color: #909399;
  font-size: 0.9rem;
  transition: all 0.3s ease;
}

.user-info:hover .dropdown-icon {
  color: #409EFF;
  transform: rotate(180deg);
}

.dropdown-menu {
  display: none;
  position: absolute;
  top: 100%;
  right: 0;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  min-width: 140px;
  z-index: 1001;
  padding: 0.5rem 0;
  border: 1px solid #ebeef5;
  animation: fadeInDropdown 0.2s ease;
}

.user-info:hover .dropdown-menu {
  display: block;
}

.dropdown-menu-item {
  display: block;
  padding: 0.7rem 1.2rem;
  color: #606266;
  text-decoration: none;
  font-size: 0.9rem;
  transition: all 0.2s ease;
}

.dropdown-menu-item:hover {
  background-color: #f5f7fa;
  color: #409EFF;
  padding-left: 1.4rem;
}

/* 模态框样式 */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 2000;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal-content {
  background: rgb(222, 241, 248);
  padding: 2rem;
  border-radius: 12px;
  width: 450px;
  position: relative;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
  animation: slideIn 0.3s ease;
  pointer-events: auto;
}

@keyframes slideIn {
  from { transform: translateY(-50px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.error-message {
  background-color: #ffebee;
  color: #d32f2f;
  padding: 0.8rem 1rem;
  border-radius: 8px;
  margin-bottom: 1.5rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
  border: 1px solid #ef9a9a;
  animation: shake 0.5s ease;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-5px); }
  40%, 80% { transform: translateX(5px); }
}

.role-tab {
  position: relative;
  display: flex;
  gap: 0;
  margin-bottom: 1.5rem;
  justify-content: center;
  position: relative;
  top: -2rem;
  height: 50px;
  width: 450px;
  left: -33px;
}

.role-tab button {
  padding: 0.8rem 2rem;
  border: none;
  background: #ecf5ff;
  color: #666;
  cursor: pointer;
  font-weight: 500;
  font-size: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  width: 90%;
  transition: all 0.3s ease;
  position: relative;
  z-index: 1;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

.role-tab button:first-child {
  clip-path: polygon(0% 0%, 80% 0%, 100% 100%, 0% 100%);
  border-top-left-radius: 8px;
  border-bottom-left-radius: 8px;
}

.role-tab button:last-child {
  clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 20% 100%);
  border-top-right-radius: 8px;
  border-bottom-right-radius: 8px;
}

.role-tab button.active1 {
  position: relative;
  transform: scale(1.2);
  left: 22px;
  top: 5px;
  background: #409EFF;
  color: white;
  z-index: 2;
  box-shadow: 0 6px 12px rgba(64, 158, 255, 0.3);
}

.role-tab button.active2 {
  position: relative;
  transform: scale(1.2);
  left: -22px;
  top: 5px;
  background: #409EFF;
  color: white;
  z-index: 2;
  box-shadow: 0 6px 12px rgba(64, 158, 255, 0.3);
}



.sub-tab {
  display: flex;
  gap: 1.5rem;
  margin-bottom: 1.5rem;
  justify-content: center;
}

.sub-tab button {
  border: none;
  background: none;
  cursor: pointer;
  font-weight: 500;
  font-size: 0.95rem;
  color: #666;
  position: relative;
  padding-bottom: 0.5rem;
  transition: color 0.3s ease;
}

.sub-tab button.active {
  color: #409EFF;
}

.sub-tab button.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 3px;
  background-color: #409EFF;
  border-radius: 2px;
}

.sub-tab button:hover {
  color: #66b1ff;
}

.tab-content {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  animation: fadeInContent 0.3s ease;
}

@keyframes fadeInContent {
  from { opacity: 0; transform: scale(0.95); }
  to { opacity: 1; transform: scale(1); }
}

.tab-content input {
  padding: 0.9rem;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  font-size: 0.9rem;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.tab-content input:focus {
  border-color: #409EFF;
  box-shadow: 0 0 5px rgba(64, 158, 255, 0.3);
  outline: none;
}

.captcha-container {
  display: flex;
  gap: 1rem;
  align-items: center;
}

.captcha-container input {
  flex: 1;
}

.captcha-image {
  width: 100px;
  height: 40px;
  border-radius: 4px;
  cursor: pointer;
  transition: opacity 0.3s ease;
}

.captcha-image:hover {
  opacity: 0.9;
}

.tab-content button {
  padding: 0.9rem;
  border: none;
  background-color: #409EFF;
  color: white;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
  font-size: 0.9rem;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.5rem;
  transition: background-color 0.3s ease, transform 0.2s ease;
}

.tab-content button:hover {
  background-color: #66b1ff;
  transform: translateY(-2px);
}

.tab-content button:disabled {
  background-color: #c0c4cc;
  cursor: not-allowed;
  transform: none;
}

.sub-tab-content {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.third-party-divider {
  display: flex;
  align-items: center;
  text-align: center;
  color: #aaa;
  font-size: 0.8rem;
  margin: 1.5rem 0 1rem 0;
}

.third-party-divider::before,
.third-party-divider::after {
  content: '';
  flex: 1;
  border-bottom: 1px solid #e0e0e0;
}

.third-party-divider:not(:empty)::before {
  margin-right: .5em;
}

.third-party-divider:not(:empty)::after {
  margin-left: .5em;
}

.third-party-login {
  display: flex;
  justify-content: center;
}

.alipay-btn {
  background-color: #fff;
  color: #1677ff;
  border: 1px solid #bae7ff;
  border-radius: 8px;
  padding: 0.8rem 1.5rem;
  cursor: pointer;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 0.8rem;
  transition: all 0.3s ease;
  width: 100%;
  justify-content: center;
  font-size: 1rem;
}

.alipay-btn:hover {
  box-shadow: 0 1px 1px rgba(22, 119, 255, 0.2);
}

.alipay-icon {
  width: 24px;
  height: 24px;
}

.forgot-password-link {
  margin-top: 10px;
  font-size: 0.85rem;
  color: #666;
  text-decoration: none;
  text-align: right;
  display: block;
}

.forgot-password-link:hover {
  color: #409EFF;
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .header-nav {
    gap: 0.8rem;
  }
  
  .header-search {
    width: 220px;
  }
}

@media (max-width: 992px) {
  .header-container {
    padding: 0 1.5rem;
  }
  
  .header-left {
    gap: 1.5rem;
  }
  
  .nav-text {
    display: none;
  }
  
  .header-search {
    width: 180px;
  }
}

@media (max-width: 768px) {
  .header-container {
    height: auto;
    flex-direction: column;
    gap: 1rem;
    padding: 1rem;
  }

  .header-left, .header-right {
    width: 100%;
    justify-content: center;
  }
  
  .header-nav {
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.5rem;
  }
  
  .header-search {
    width: 100%;
    max-width: 400px;
  }
  
  .nav-text {
    display: inline;
  }
  
  .dropdown-content {
    position: static;
    display: none;
    width: 100%;
    box-shadow: none;
    border-radius: 0;
    border-top: 1px solid #eee;
    border-bottom: 1px solid #eee;
    margin-top: 0.5rem;
  }

  .nav-dropdown:hover .dropdown-content {
    display: block;
  }
  
  .modal-content {
    width: 90%;
    max-width: 400px;
  }
  
  .role-tab {
    width: 100%;
    left: 0;
  }
  
  .role-tab button {
    width: 100%;
  }
  
  .role-tab button.active1,
  .role-tab button.active2 {
    transform: scale(1);
    left: 0;
    top: 0;
  }
}
</style>